<script setup>
import * as api from "@/api/v-sys-service/exam/group";
import EXAM_QUESTION from "@/views/v-sys-service/exam/question/index.vue"
import EXAM_TEMPLATE from "@/views/v-sys-service/exam/template/index.vue"
import examQuestionGroupFormDialog from "@/views/v-sys-service/exam/group/group-form-dialog.vue"
/**
 * 组件挂载后
 */
onMounted(() => {
  init()
})

const activeName = ref("EXAM_QUESTION")

/**
 * 创建浅响应式对象、不会对组件对象深度观察、避免不必要的性能开销、移除控制台警告
 */
const activeComponent = shallowRef(EXAM_QUESTION)

const tabs = ref([
  { label: "试题管理", name: "EXAM_QUESTION", component: shallowRef(EXAM_QUESTION) },
  { label: "试卷管理", name: "EXAM_TEMPLATE", component: shallowRef(EXAM_TEMPLATE) },
])

/**
 * 列表数据
 */
const groupData = ref([])

const groupIndex = ref(0)

const componentKey = ref()

/**
 * loading状态
 */
const loading = ref({
  tableLoading: false,// 列表loading
})

/**
 * 弹窗显隐
 */
const dialog = ref({
  showFormDialog: false, //表单弹窗
})

const rowData = ref({})

/**
 * 分页参数
 */
const pageQuery = reactive({
  page: 1,
  limit: 50,
  total: 0,
  url: '/cloud-sys-service/exam-question-group/page',
  orders: [
    {column: "createTime", asc: false}
  ],
  filters: [],
  condition: {
    key: "",
  },
})

/**
 * 组件初始化调用函数
 */
const init = async () => {
  await groupQuery()
  rowData.value = groupData.value[groupIndex.value]
  componentKey.value = new Date().getTime()
}

/**
 * 分页查询
 */
const groupQuery = async () => {
  loading.value.tableLoading = true
  const { code, data } = await api.page(pageQuery)
  if (code === 200) {
    loading.value.tableLoading = false
    groupData.value = data.records
    pageQuery.total = data.total
  }
}

/**
 * 选择分组
 */
const chooseGroup = (item, index) => {
  groupIndex.value = index
  rowData.value = item
  componentKey.value = new Date().getTime()
}

const tabChange = (val) => {
  activeName.value = val.paneName
  tabs.value.forEach(ele => {
    if (ele.name === val.paneName) activeComponent.value = ele.component
  })
}

/**
 * form表单
 */
const showFormDialog = ($event,row) => {
  if (row && row.id) rowData.value = row
  else rowData.value = {}
  dialog.value.showFormDialog = true
}

/**
 * 删除
 */
const showRemoveDialog = ($event,row) => {
  ElMessageBox.confirm('', '确定删除吗',
      {
        closeOnClickModal: false,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        beforeClose: (action, instance, done) => {
          if (action === 'confirm') {
            instance.confirmButtonLoading = true
            instance.confirmButtonText = '拼命加载中...'
            api.removeById(row.id).then(({code})=>{
              instance.confirmButtonLoading = false
              if (code === 200){
                ElMessage({message: '删除成功', type: 'success',})
                groupQuery()
              }
            })
            done()
          } else {
            done()
          }
        }
      }
  )
}

/**
 * 组件对外提供可访问数据
 */
defineExpose({
  showFormDialog,
  showRemoveDialog,
})

</script>

<template>
  <div class="box">
    <div class="left">
     <div class="group-search">
        <el-input
             placeholder="请输入关键字"
             v-model="pageQuery.condition.key"
            @keyup.enter="groupQuery" @clear="groupQuery" clearable
        ></el-input>
        <div class="search-box" @click="groupQuery">
          <el-icon><i-ep-search/></el-icon>
        </div>
     </div>
      <ul class="group-list">
        <li
            v-for="(item, index) in groupData"
            :key="index"
            @click="chooseGroup(item, index)"
            :class="groupIndex === index ? 'li-active' : ''"
        >
          <div class="flex align-center">
            <v-svg-icon icon-class="note-book" />
            <div class="ellipsis"> {{ item.name }}</div>
          </div>
          <el-dropdown>
            <span class="el-dropdown-link">
              <el-icon><i-ep-more /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="showFormDialog($event, item)">重命名</el-dropdown-item>
                <el-dropdown-item @click.native="showFormDialog">新增分组</el-dropdown-item>
                <el-dropdown-item @click.native="showRemoveDialog($event, item)">删除该组</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </li>
      </ul>
    </div>
    <div class="right">
      <el-tabs v-model="activeName" class="exam-tabs" @tab-click="tabChange">
        <el-tab-pane v-for="item in tabs" :label="item.label" :name="item.name" />
        <component :is="activeComponent" :rowData="rowData" :key="componentKey" />
      </el-tabs>
    </div>
    <!--  题库分组表单    -->
    <examQuestionGroupFormDialog v-if="dialog.showFormDialog" @query="groupQuery" v-model="dialog.showFormDialog" :row-data="rowData" />
  </div>
</template>

<style scoped lang="scss">
.box {
  display: flex;
  .left {
    min-width: 261px;
    width: 261px;
    height: calc(100vh - 85px);
    border-right: 1px solid #eeeeee;
    overflow-y: auto;
    overflow-x: hidden;
    .group-search {
      display: flex;
      margin: 13px 9px;
      .search-box {
        width: 37px;
        height: 32px;
        line-height: 32px;
        background: #00a7ff;
        border-top-right-radius: 2px;
        border-bottom-right-radius: 2px;
        text-align: center;
        color: #ffffff;
        font-size: 16px;
        cursor: pointer;
      }
    }
    .group-list {
      li {
        display: flex;
        align-items: center;
        justify-content: space-between;
        height: 49px;
        line-height: 49px;
        cursor: pointer;
        color: #1f2635;
        font-size: 14px;
        padding: 0 20px;
        border-bottom: 1px solid #eeeeee;
        .ellipsis {
          width: 185px;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }
      }
      .li-active {
        background: rgba(255, 106, 0, 0.1);
        color: #00a7ff;
        .el-icon {
          color: #00a7ff;
        }
      }
    }
  }
  .right {
    height: 100%;
    width: calc(100% - 261px);
    :deep(.el-tabs) {
      .el-tabs__header {
        margin: 10px 0 0;
        padding: 0 20px;
        .el-tabs__nav-wrap::after {
          content: unset;
        }
        .el-tabs__active-bar {
          height: 0;
        }
      }
      .el-tabs__content {
        overflow: unset;
      }
    }
  }
}
</style>
